********************************************************************************
*program for more general tables. (instead of audit variables, use indices, for example.)
********************************************************************************

*Settings
clear all
cap log close
*Change this to your system name
local user "awcassidy1"
set more off, permanently
cd "C:\Users\awcassidy1\Dropbox\jmp_new\cleaned_data"

use "C:\Users\awcassidy1\Dropbox\jmp_new\cleaned_data/data_with_observability_indices.dta", clear




********************************************************************************
*Baseline: observability groups
********************************************************************************
*standard values
local clustvar "mlsid"
local clustvarword "Home"
local fevars "i.saleyear i.lsaleyear i.yearbuiltcat"
local fe_to_absorb="saleyear lsaleyear yearbuiltcat"
local controls "yearbuiltaudit conditionedsqft"
local baselinevars "parsimonious_less parsimonious_more_fuel parsimonious_more_other"
local filename "main"
local depvar "pricediff"
local not_extracted="duct_type  rsavg  eeravg  sizeavg    negsysageavg ah_type notwinrec  notraddr"

local parsimonious_less_vars "negleakavg atticrvalue ductravg     "
local parsimonious_more_vars_fuel "fftype whftype "
local parsimonious_more_vars_other "ah_location progtherm twosystems"


********************************************************************************
*VAR DEFINITIONS AND LABELS
********************************************************************************
gen pre_above_med_ee=above_median_ee_all_mean*pre
gen post_above_med_ee= above_median_ee_all_mean*post
la var pre_above_med_ee "Above Med EE $\times$ Pre-sample"
la var post_above_med_ee "Above Med EE $\times$ Post"
la var above_median_ee_all_mean "Above Med EE"
la var above_median_less "Above Med Less"

gen pre_above_med_less=above_median_less*pre
gen post_above_med_less= above_median_less*post
la var pre_above_med_less "Above Med Less $\times$ Pre-sample"
la var post_above_med_less "Above Med Less $\times$ Post"

gen pre_Less_st = Less_st*pre
gen post_Less_st = Less_st*post
la var pre_Less_st "Less $\times$ Pre"
la var post_Less_st "Less $\times$ Post"

gen pre_More_Fuel_st = More_Fuel_st*pre
gen post_More_Fuel_st = More_Fuel_st*post
la var pre_More_Fuel_st "More-Fuel $\times$ Pre"
la var post_More_Fuel_st "More-Fuel $\times$ Post"

gen pre_More_Other_st = More_Other_st*pre
gen post_More_Other_st = More_Other_st*post
la var pre_More_Other_st "More-Other $\times$ Pre"
la var post_More_Other_st "More-Other $\times$ Post"

********************************************************************************
*Additional Analyses (Table A6)
********************************************************************************
eststo clear



********************************************************************************
*list pricediff
********************************************************************************
*list pricediff
eststo clear

eststo: reghdfe list_pricediff Less_st ///
	, ///
		vce(cl mlsid) absorb(yearbuiltcat saleyear lsaleyear )	

estadd scalar r2all = e(r2)


estadd local sample="\multicolumn{1}{c}{Main}"
estadd local dep_var = "\multicolumn{1}{c}{$\Delta$ LP}"
estadd local stdzd = "\multicolumn{1}{c}{Yes}"
estadd local control_yblt_sqft =  "\multicolumn{1}{c}{Yes}"
estadd local control_not_extracted =  "\multicolumn{1}{c}{No}"
estadd local control_wh_tanktype =  "\multicolumn{1}{c}{No}"
estadd local control_bedsbathspool =  "\multicolumn{1}{c}{No}"

*list pricediff for sample who got audit after listing.

eststo: reghdfe list_pricediff Less_st  if audit_before_list==0 , ///
		vce(cl mlsid) absorb(yearbuiltcat saleyear lsaleyear )	

estadd scalar r2all = e(r2)


estadd local sample="\multicolumn{1}{c}{Audit After List}"
estadd local dep_var = "\multicolumn{1}{c}{$\Delta$ LP}"
estadd local stdzd = "\multicolumn{1}{c}{Yes}"
estadd local control_yblt_sqft =  "\multicolumn{1}{c}{Yes}"
estadd local control_not_extracted =  "\multicolumn{1}{c}{Yes}"
estadd local control_wh_tanktype =  "\multicolumn{1}{c}{No}"
estadd local control_bedsbathspool =  "\multicolumn{1}{c}{No}"

*likelihood of audit before list
eststo: reghdfe audit_before_list Less_st ///
	, ///
		vce(cl mlsid) absorb(yearbuiltcat saleyear lsaleyear )	

estadd scalar r2all = e(r2)


estadd local sample="\multicolumn{1}{c}{Main}"
estadd local dep_var = "\multicolumn{1}{c}{$\mathbbm{1}\left\lbrace \text{Audit Before List} \right\rbrace $}"
estadd local stdzd = "\multicolumn{1}{c}{Yes}"
estadd local control_yblt_sqft =  "\multicolumn{1}{c}{Yes}"
estadd local control_not_extracted =  "\multicolumn{1}{c}{Yes}"
estadd local control_wh_tanktype =  "\multicolumn{1}{c}{Yes}"
estadd local control_bedsbathspool =  "\multicolumn{1}{c}{No}"


*seller_surplus

eststo: reghdfe seller_surplus Less_st  ///
	, ///
		vce(cl mlsid) absorb(yearbuiltcat saleyear lsaleyear )	

estadd scalar r2all = e(r2)


estadd local sample="\multicolumn{1}{c}{Main}"
estadd local dep_var = "\multicolumn{1}{c}{$\Delta$ LP - P}"
estadd local stdzd = "\multicolumn{1}{c}{Yes}"
estadd local control_yblt_sqft =  "\multicolumn{1}{c}{Yes}"
estadd local control_not_extracted =  "\multicolumn{1}{c}{Yes}"
estadd local control_wh_tanktype =  "\multicolumn{1}{c}{Yes}"
estadd local control_bedsbathspool =  "\multicolumn{1}{c}{Yes}"

*time on market

eststo: reghdfe time_on_market_diff Less_st  ///
	, ///
		vce(cl mlsid) absorb(yearbuiltcat saleyear lsaleyear )	

estadd scalar r2all = e(r2)


estadd local sample="\multicolumn{1}{c}{Main}"
estadd local dep_var = "\multicolumn{1}{c}{$\Delta$ TOM}"
estadd local stdzd = "\multicolumn{1}{c}{Yes}"
estadd local control_yblt_sqft =  "\multicolumn{1}{c}{Yes}"
estadd local control_not_extracted =  "\multicolumn{1}{c}{Yes}"
estadd local control_wh_tanktype =  "\multicolumn{1}{c}{Yes}"
estadd local control_bedsbathspool =  "\multicolumn{1}{c}{Yes}"


local keepvars Less_st 

		 
esttab using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/additional_analyses_less_only.tex", replace fragment ///
		keep(`keepvars') order(`keepvars') ///
		 label booktabs nomtitles noobs se(4) b(4) ///
		scalars( ///
					"sample Sample" ///
					"dep_var Dep Var" ///
				"r2all R-squared" ///
				 "N Observations"  ///
				) ///
				sfmt( 0 0 %9.3fc %9.0fc ) star(* 0.10 ** 0.05 *** 0.01)
				
********************************************************************************				
*Hotness (Table A7)
********************************************************************************				


la var hot_avg_price_dollars "Avg Price"
la var hot_sales "Sales"
la var hot_volume_dollars "Volume"
la var hot_median_price_dollars "Median Price"
la var hot_total_listings "Tot Listings"
la var hot_months_of_inventory "Mos Inventory"

eststo clear
		
foreach v of varlist  hot_* {
	sum `v'
	gen `v'_st=(`v'-r(mean))/r(sd)
	la var `v'_st "`: var label `v''"
	}
	
cap gen interaction=.
la var interaction "Less $\times$ Hotness"

cap gen hotness=.
la var hotness "Hotness"

*these are not valid to use
*cap drop hot_avg_price_dollars_st



foreach v of varlist  hot_*_st {
	
	di "v=`v'"
	replace interaction=Less_st*`v'
	replace hotness=`v'

eststo: reghdfe pricediff interaction Less_st hotness ///
	, ///
		vce(cl mlsid) absorb(yearbuiltcat saleyear lsaleyear )	
		

	*estadd local sample="\multicolumn{1}{c}{Main}"
	estadd local hot_var = "\multicolumn{1}{c}{`: var label `v''}"
	estadd local control_yblt_sqft =  "\multicolumn{1}{c}{Yes}"
	estadd local control_not_extracted =  "\multicolumn{1}{c}{Yes}"
	estadd local control_wh_tanktype =  "\multicolumn{1}{c}{Yes}"
	estadd local control_bedsbathspool =  "\multicolumn{1}{c}{Yes}"		
estadd scalar r2all = e(r2)

				}
				
local keepvars= "Less_st interaction hotness"
				
esttab using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/hotness_less_only.tex", replace fragment ///
		keep(`keepvars') order(`keepvars') ///
		 label booktabs nomtitles noobs se(4) b(4) ///
		scalars( ///
					"hot_var Hotness Var" ///
				"r2all R-squared" ///
				 "N Observations"  ///
				) ///
				sfmt( 0 %9.3fc %9.0fc ) star(* 0.10 ** 0.05 *** 0.01)